package com.carcenter.order.service.impl;
import java.math.BigDecimal;
import java.time.LocalDateTime;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.carcenter.common.constant.product.ProductConstant;
import com.carcenter.common.exception.Assert;
import com.carcenter.common.model.dto.ProductQueryDto;
import com.carcenter.common.model.dto.SaveProductDto;

import com.carcenter.common.model.vo.CarModelVo;
import com.carcenter.common.model.vo.ProductVo;
import com.carcenter.common.result.Result;
import com.carcenter.common.result.enums.impl.BusinessCode;
import com.carcenter.order.entity.Products;
import com.carcenter.order.entity.Specifications;
import com.carcenter.order.entity.VehicleModels;
import com.carcenter.order.mapper.ProductsMapper;
import com.carcenter.order.mapper.VehicleModelsMapper;
import com.carcenter.order.service.IProductsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.carcenter.order.service.ISpecificationsService;
import com.carcenter.order.service.IVehicleModelsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author car
 * @since 2025-07-16
 */
@Service
public class ProductsServiceImpl extends ServiceImpl<ProductsMapper, Products> implements IProductsService {
    @Autowired
    private ProductsMapper productsMapper;
    @Autowired
    private ISpecificationsService specificationsService;
    @Autowired
    private IVehicleModelsService vehicleModelsService;
    @Autowired
    private VehicleModelsMapper vehicleModelsMapper;
    @Override
    public IPage<ProductVo> getList(Integer pageNum, Integer pageSize, ProductQueryDto queryDto) {
        IPage<ProductVo> page = new Page<>(pageNum,pageSize);
        return productsMapper.getProducts(page,queryDto);
    }

    @Override
    @Transactional
    public Result saveProduct(SaveProductDto saveProductDto) {
        Specifications specifications = new Specifications();
        Products products = new Products();
        BeanUtil.copyProperties(saveProductDto,specifications);
        specificationsService.save(specifications);
        BeanUtil.copyProperties(saveProductDto,products);
        products.setProductCode(UUID.randomUUID().toString().replaceAll("-","").substring(10));
        products.setProductBarcode(UUID.randomUUID().toString().replaceAll("-","").substring(10));
        products.setSpecificationId(specifications.getId());
        products.setCreateTime(LocalDateTime.now());
        products.setLifecycle(ProductConstant.SALE);
        save(products);
        return Result.ok();
    }

    @Override
    @Transactional
    public Result editProduct(SaveProductDto saveProductDto) {
        Specifications specifications = new Specifications();
        Products products = new Products();
        BeanUtil.copyProperties(saveProductDto,specifications);
        specifications.setId(saveProductDto.getSpecificationId());
        specificationsService.updateById(specifications);
        BeanUtil.copyProperties(saveProductDto,products);
        updateById(products);
        return Result.ok();
    }

    @Override
    @Transactional
    public Result match(Integer id) {
        LambdaQueryWrapper<Products> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Products::getId,id);
        Products products = getOne(queryWrapper);

        LambdaQueryWrapper<Specifications> specificationsQueryWrapper = new LambdaQueryWrapper<>();
        specificationsQueryWrapper.eq(Specifications::getId,products.getSpecificationId());
        Specifications specifications = specificationsService.getOne(specificationsQueryWrapper);

        List<CarModelVo> list = vehicleModelsMapper.getCarModel(specifications.getViscosity());

        if (list.size()>0){
            return Result.ok(list);
        }
        return Result.fail(BusinessCode.NO_MATCHING_MODELS);
    }
}
